/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pojo.LoaiSanPham_pojo;
import util.MySQLDataAccessHelper;

/**
 *
 * @author Rocker000
 */
public class LoaiSanPham_dao 
{
    public static LoaiSanPham_pojo LayLoaiTheoMa(String maloai)
    {
        LoaiSanPham_pojo temp = new LoaiSanPham_pojo();
        try
        {
            String sql = "select*from loaisanpham where maloaisanpham='"+maloai+"'";
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                
                temp.setMaLoaiSanPham(rs.getString("maloaisanpham"));
                temp.setMaDanhMuc(rs.getString("madanhmuc"));
                temp.setTenLoaiSanPham(rs.getString("tenloaisanpham"));
                temp.setImage(rs.getString("Image"));
                
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return temp;
    }
    
    public static ArrayList<LoaiSanPham_pojo> LayDanhSach(String madm)
    {
        ArrayList<LoaiSanPham_pojo> ds = new ArrayList<LoaiSanPham_pojo>();
        try
        {
            String sql = "select*from loaisanpham where madanhmuc='"+madm+"'";
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                LoaiSanPham_pojo temp = new LoaiSanPham_pojo();
                temp.setMaLoaiSanPham(rs.getString("maloaisanpham"));
                temp.setMaDanhMuc(madm);
                temp.setTenLoaiSanPham(rs.getString("tenloaisanpham"));
                temp.setImage(rs.getString("Image"));
                ds.add(temp);
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    
    public static ArrayList<LoaiSanPham_pojo> LayToanBoDanhSach()
    {
        ArrayList<LoaiSanPham_pojo> ds = new ArrayList<LoaiSanPham_pojo>();
        try
        {
            String sql = "select*from loaisanpham ";
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                LoaiSanPham_pojo temp = new LoaiSanPham_pojo();
                temp.setMaLoaiSanPham(rs.getString("maloaisanpham"));
                temp.setMaDanhMuc(rs.getString("madanhmuc"));
                temp.setTenLoaiSanPham(rs.getString("tenloaisanpham"));
                temp.setImage(rs.getString("Image"));
                ds.add(temp);
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    
    public static String LayKichThuoc(String maloaisanpham)
    {
        String temp = null;
        String sql = "select*from loaisanpham,donvi where loaisanpham.kichthuoc=donvi.madonvi and maloaisanpham ='"+maloaisanpham+"'";
        try
        {
            
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                temp = rs.getString("tendonvi");
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return temp;
    }
    
    public static float GetMaxSize(String maloaisanpham)
    {
        float max = 0;
        String sql ="select*from sanpham where masanpham='"+maloaisanpham+"'";
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                float temp = rs.getFloat("kichthuoc");
                if(temp>=max)max=temp;
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return max;
    }
    public static float GetMixSize(String maloaisanpham)
    {
        float min = 0;
        String sql ="select*from sanpham where masanpham='"+maloaisanpham+"'";
        try
        {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.ExeCuteQuery(sql);
            while(rs.next())
            {
                float temp = rs.getFloat("kichthuoc");
                if(temp<=min)min=temp;
            }
            helper.Close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return min;
        
    }
    
}
